package com.stadium.controller;

import java.util.List;

import com.stadium.common.BaseResponse;
import com.stadium.dto.*;
import com.stadium.entity.GameEvent;
import com.stadium.entity.TeamRecord;
import com.stadium.service.GameEventSV;
import com.stadium.service.GameTeamSV;
import com.stadium.service.RTeamUserSV;
import com.stadium.vo.GameTeamVo;
import com.stadium.vo.searchVo.GameEventSearchVo;
import com.stadium.vo.searchVo.GameTeamSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 球队详情
 *
 * @author wsl
 * @email
 * @date 2021-05-04 09:39:09
 */
@RestController
@RequestMapping("/api/gameteam")
@Api(tags = "球队详情")
public class GameTeamCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GameTeamSV gameTeamSV;

    @Autowired
    private RTeamUserSV rTeamUserSV;
    @Autowired
    private GameEventSV gameEventSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@RequestBody @Validated GameTeamVo gameTeamVo) {
        try {
            if (gameTeamVo.getId() == null ||gameTeamVo.getId() == ""){
                    gameTeamSV.save(gameTeamVo);
            }else{
                    gameTeamSV.update(gameTeamVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "转交管理员权限")
    @PostMapping(value = "/v1/adminPassOnTo")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId"),
            @ApiImplicitParam(name="userId",value="userId,转交人id")})
    public BaseResponse adminPassOnTo(@RequestParam(name = "teamId",required = true) String teamId,
                                      @RequestParam(name = "userId",required = true) String userId) {
        try {
            gameTeamSV.adminPassOnTo(teamId,userId);

            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            gameTeamSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    /*@ApiOperation(value = "批量删除")
    @PostMapping(value = "/v1/deleteBatch")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value="ids")})
    public BaseResponse deleteBatch(@RequestParam(name = "ids", required = true)String[] ids) {
        try {
            gameTeamSV.deleteBatch(ids);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }*/

    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<GameTeamDto>> query(@Validated GameTeamSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            if(StringUtils.isEmpty(searchVo.getColumnName())){
                searchVo.setColumnName("create_time");
            }
            if(StringUtils.isEmpty(searchVo.getDir())){
                searchVo.setDir("DESC");
            }
            List<GameTeamDto> queryList = gameTeamSV.query(searchVo);
            int total = gameTeamSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "移动端球队首页")
    @GetMapping(value = "/v1/list")
    public BaseResponse<List<TeamAndEventDto>> list(@RequestParam(name = "userId", required = true)String userId) {
        try {

            List<TeamAndEventDto> queryList = gameTeamSV.getListByUserId(userId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }




    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<GameTeamDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                GameTeamDto queryData = gameTeamSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得球队队员", notes = "获得球队队员")
    @GetMapping(value = "/v1/getUsersByTeamId")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId")})
    public BaseResponse<List<SysUserDto1>> getUsersByTeamId(@RequestParam(name = "teamId", required = true)String teamId) {
        try {
            List<SysUserDto1> queryData = rTeamUserSV.getUsersByTeamId(teamId);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }


    @ApiOperation(value = "球队赛事-赛事列表")
    @GetMapping(value = "/v1/queryEventsByTeamId")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId")})
    public BaseResponse<List<GameEventWithScheduleDto>> queryEventsByTeamId(@RequestParam(name = "teamId", required = true)String teamId) {
        try {
            List<GameEventWithScheduleDto> queryList = gameTeamSV.queryEventsByTeamId(teamId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "我的赛事-赛事列表")
    @GetMapping(value = "/v1/queryEventsByUserId")
    @ApiImplicitParams({@ApiImplicitParam(name="userId",value="userId")})
    public BaseResponse<List<GameEventDto>> queryEventsByUserId(@RequestParam(name = "userId", required = true)String userId) {
        try {
            GameEventSearchVo gameEventSearchVo = new GameEventSearchVo();
            gameEventSearchVo.setUserId(userId);
            List<GameEventDto> queryList = gameEventSV.query(gameEventSearchVo);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }


    @ApiOperation(value = "球队历史")
    @GetMapping(value = "/v1/TeamLog")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId")})
    public BaseResponse<List<TeamLogDto>> TeamLog(@RequestParam(name = "teamId", required = true)String teamId) {
        try {
            List<TeamLogDto> queryList = gameTeamSV.TeamLog(teamId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球队战绩")
    @GetMapping(value = "/v1/TeamRecord")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId")})
    public BaseResponse<TeamRecord> TeamRecord(@RequestParam(name = "teamId", required = true)String teamId) {
        try {
            TeamRecord queryList = gameTeamSV.TeamRecord(teamId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
